package com.ff.xyh.service.complaint.controller;


import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.qo.complaint.ComplaintHandleQuery;
import com.ff.xyh.common.entity.complaint.ComplaintHandle;
import com.ff.xyh.service.complaint.service.ComplaintHandleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ff
 * @since 2023-05-24
 */
@RestController
@RequestMapping("/complaint-service")
public class ComplaintHandleController {

    @Autowired
    private ComplaintHandleService complaintHandleService;

    /**
     * 举报处理信息 - 条件查询，包含分页
     * @param complaintHandleQuery
     * @return
     */
    @PostMapping("get-complaint-handle-condition")
    public ResultObject getComplaintHandleCondition(
            @RequestBody ComplaintHandleQuery complaintHandleQuery) {
        return complaintHandleService.getComplaintHandleCondition(complaintHandleQuery);
    }

    /**
     * 举报处理信息 - 根据 id 删除
     * @param id
     * @return
     */
    @PostMapping("delete-complaint-handle-id")
    public ResultObject deleteComplaintHandleById(
            @RequestParam("id") String id) {
        if (complaintHandleService.getById(id) == null) {
            return ResultObject.error().message(ResultMessage.ERROR_ARG);
        } else {
            if (complaintHandleService.removeById(id)) {
                return ResultObject.ok();
            } else {
                return ResultObject.ok().message(ResultMessage.FAILED_TO_DELETE);
            }
        }
    }

    /**
     * 举报处理信息 - 添加
     * @param complaintHandle
     * @return
     */
    @PostMapping("add-complaint-handle")
    public ResultObject addComplaintHandle(
            @RequestBody ComplaintHandle complaintHandle) {
        if (!StringUtils.isEmpty(complaintHandle.getId())) {
            return ResultObject.error().message(ResultMessage.ERROR_ARG);
        }
        if (complaintHandleService.save(complaintHandle)) {
            return ResultObject.ok().putData(complaintHandle);
        } else {
            return ResultObject.error().message(ResultMessage.FAILED_TO_INSERT);
        }
    }


    /**
     * 举报处理信息 - 修改
     * @param complaintHandle
     * @return
     */
    @PostMapping("modify-complaint-handle")
    public ResultObject modifyComplaintHandle(
            @RequestBody ComplaintHandle complaintHandle) {
        if (StringUtils.isEmpty(complaintHandle.getId())
                || complaintHandleService.getById(complaintHandle.getId()) == null) {
            return ResultObject.error().message(ResultMessage.ERROR_ARG);
        }
        if (complaintHandleService.updateById(complaintHandle)) {
            return ResultObject.ok();
        } else {
            return ResultObject.error().message(ResultMessage.FAILED_TO_UPDATE);
        }
    }
}

